using System.Collections;
using System.Collections.Generic;
using BCW;
using Prime31;
using UnityEngine;
using UnityEngine.SceneManagement;

public class controllerMenu : MonoBehaviour
{
	public static controllerMenu thisScript;

	public UIPanel panelMainMenu;

	public UIPanel panelHeroRoom;

	public UIPanel panelSettings;

	public UIPanel panelMessage;

	public UIPanel blockPanel;

	public UIPanel panelControlsSettings;

	public GameObject objGrid;

	public GameObject colliderIndicator;

	public GameObject panelInfoAfterTutorial;

	public GameObject indicatorExp;

	public GameObject parentLobbyUI;

	public GameObject panelAchievement;

	public UILabel lbMessage;

	private bool clickOnButton;

	public RotationObject rotatePlayer;

	public WebViewObject wvo;

	private TouchScreenKeyboard mKeyboard;

	private AudioSource mainMusic;

	private bool showTut;

	public static string keyDaysTimer = "keyDaysTimer";

	[SerializeField]
	private GameObject disconnect;

	[SerializeField]
	private GameObject connect;

	[SerializeField]
	private GameObject noConnection;

	public IEnumerator cns;

	private bool loadFromGameScene;

	private bool adsAlreadyShown;

	private Coroutine _showHeroRoom;

	private Coroutine _showSettings;

	private Coroutine _showControllSettings;

	public bool ClickOnButton
	{
		get
		{
			return clickOnButton;
		}
		set
		{
			clickOnButton = value;
		}
	}

	private void Awake()
	{
		thisScript = this;
		mainMusic = GetComponent<AudioSource>();
		FyberWrapper.adsInProgress += AdsInProgressEventHandler;
		settings.updateOfflineMode(false);
		CupsManager.LoadLobby();
	}

	private void Start()
	{
		StartCheckNetwork();
		if (GpgWrapper.instance != null)
		{
			GpgWrapper.instance.SaveStars();
			GpgWrapper.instance.SaveProducts();
		}
		BCWDebug.Log("Load from game: " + settings.loadFromGame, "Fyber");
		InGameTutorial.StartTutorial("new_lobby_tutorial", panelMainMenu.transform, string.Empty);
		if (BCWStorage.Get<string>("new_lobby_tutorial").Equals("Ended"))
		{
			InGameTutorial.StartTutorial("new_tutorial_gotcha", thisScript.panelMainMenu.transform, string.Empty);
		}
		if (!showTut)
		{
			InGameTutorial.StartTutorial("new_tutorial", panelMainMenu.transform, string.Empty);
			showTut = true;
		}
		QualityController.InstIfNeed();
		if (settings.includePreloadingSectors)
		{
			Application.backgroundLoadingPriority = ThreadPriority.High;
		}
		StatistickFromMaps();
		StartCoroutine(LoadingPlayerOnStart());
		string text = BCWStorage.Get<string>(settings.keyCryptoName);
		if (text.Equals("Player") || text.Equals(string.Empty) || settings.tekName.Length > 19)
		{
			StartCoroutine(LoadingCreateNickName());
		}
		LeaderBoardParser.ResetInstance();
		LeaderBoardServer.instance.LoadLeaderBoardTypeGameInfo();
		LeaderBoardServer.instance.LoadLeaderBoardCommonInfo();
		LeaderBoardServer.instance.GetPlayerInfo();
		LeaderBoardServer.instance.GetPlayerCommonInfo();
		settings.SetAreaNameForLoading(string.Empty);
		CheckDaysTimer();
	}

	public void CheckDaysTimer()
	{
		if (!JSONManager.Instance.ServerTimeLoad() || !FlurryWrapper.activePlayer)
		{
			return;
		}
		if (!CryptoPlayerPrefs.HasKey(keyDaysTimer))
		{
			BCWStorage.Set(keyDaysTimer, JSONManager.Instance.GetServerTime());
		}
		if (BCWStorage.Get<int>(keyDaysTimer) + settings.DayToSeconds(1) < JSONManager.Instance.GetServerTime())
		{
			int num = BCWStorage.Get<int>(FlurryWrapper.keyDayCash);
			int num2 = BCWStorage.Get<int>(FlurryWrapper.keyDayGold);
			if (num > 0)
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("24Hour", FlurryWrapper.MoneyForSandboxDay(num));
				FlurryWrapper.LogEvent(FlurryWrapper.EV_DAILY_EARNED_GOLD_PER_DAY, dictionary);
			}
			if (num2 > 0)
			{
				Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
				dictionary2.Add("24Hour", FlurryWrapper.GoldForSandboxDay(num2));
				FlurryWrapper.LogEvent(FlurryWrapper.EV_DAILY_EARNED_GOLD_PER_DAY, dictionary2);
			}
			BCWStorage.Set(keyDaysTimer, JSONManager.Instance.GetServerTime());
		}
	}

	private void AdsInProgressEventHandler(bool isAdsInProgress)
	{
		BCWDebug.Log("isAdsInProgress: " + isAdsInProgress, "Fyber");
		if (isAdsInProgress)
		{
			Time.timeScale = 0f;
			if (mainMusic != null)
			{
				mainMusic.Pause();
			}
		}
		else
		{
			Time.timeScale = 1f;
			if (mainMusic != null)
			{
				mainMusic.Play();
			}
		}
	}

	private IEnumerator LoadingCreateNickName()
	{
		yield return new WaitForSeconds(1f);
		ResourceRequest loadPlayer = Resources.LoadAsync("Prefabs/Lobby/RandomNickName");
		loadPlayer.allowSceneActivation = true;
		yield return loadPlayer;
		GameObject tmp = Object.Instantiate(loadPlayer.asset as GameObject);
		tmp.transform.parent = parentLobbyUI.transform;
		tmp.transform.localScale = Vector3.one;
	}

	private IEnumerator LoadingPlayerOnStart()
	{
		yield return new WaitForSeconds(1f);
		while (ReputationProductRewardWindow.instance.isActive)
		{
			yield return null;
		}
		ResourceRequest loadPlayer = Resources.LoadAsync("Prefabs/Lobby/LobbyPlayerController");
		loadPlayer.allowSceneActivation = true;
		yield return loadPlayer;
		GameObject tmp = Object.Instantiate(loadPlayer.asset as GameObject);
		rotatePlayer.rotateObject = tmp.GetComponent<GameObjectForRotation>().playerRotation;
		rotatePlayer.gameObject.SetActive(true);
	}

	public IEnumerator ShowRateUs()
	{
		int hoursForReview = 24;
		int launchCount = 5;
		EtceteraAndroid.askForReview(launchCount, 0, hoursForReview, "Do you like Block City Wars?", "If you do - please post a review for this app!");
		yield return null;
	}

	private void StatistickFromMaps()
	{
		List<string> list = new List<string>();
		List<int> list2 = new List<int>();
		if (Load.LoadStringArray(settings.KeyTopUsedWeaponName) == null || Load.LoadIntArray(settings.KeyTopUsedWeaponCount) == null)
		{
			return;
		}
		list.AddRange(Load.LoadStringArray(settings.KeyTopUsedWeaponName));
		list2.AddRange(Load.LoadIntArray(settings.KeyTopUsedWeaponCount));
		if (list.Count > 0 && list2.Count > 0)
		{
			int num = 0;
			int index = 0;
			for (int i = 0; i < list.Count; i++)
			{
				if (list2[i] > num)
				{
					num = list2[i];
					index = i;
				}
			}
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("Last Top Weapon: " + list[index]);
			}
			if (!string.IsNullOrEmpty(Load.LoadString(settings.KeyLastGameMode)))
			{
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add(Load.LoadString(settings.KeyLastGameMode), list[index]);
				dictionary.Add("Total (modes - pvp maps", list[index]);
				FlurryWrapper.LogEvent(FlurryWrapper.EV_TOP_USED_WEAPON_MODES, dictionary);
			}
		}
		Save.Delete(settings.KeyTopUsedWeaponName);
		Save.Delete(settings.KeyTopUsedWeaponCount);
	}

	public IEnumerator CheckNetworkStatus()
	{
		yield return StartCoroutine(VersionSelector.DownloadNeutrinoSignaturesCoroutine(this));
		yield return null;
		if (!PhotonNetwork.connected)
		{
			controllerConnectPhoton.thisScript._initializeWorldwide();
		}
		yield return new WaitForSeconds(2f);
		while (true)
		{
			if (disconnect != null)
			{
				disconnect.SetActive(!PhotonNetwork.connected);
			}
			if (!PhotonNetwork.connected)
			{
				controllerConnectPhoton.thisScript._initializeWorldwide();
			}
			yield return new WaitForSeconds(10f);
		}
	}

	public void StartCheckNetwork()
	{
		if (cns == null)
		{
			cns = CheckNetworkStatus();
			StartCoroutine(cns);
		}
	}

	public void ShowNoInternet()
	{
		if (noConnection != null)
		{
			noConnection.SetActive(true);
			Invoke("HideNoInternet", 2f);
		}
	}

	public void HideNoInternet()
	{
		if (noConnection != null)
		{
			noConnection.SetActive(false);
		}
	}

	private void Update()
	{
		if (mKeyboard != null)
		{
			if (mKeyboard.done && !mKeyboard.wasCanceled)
			{
				closeKeyboard();
			}
			else if (mKeyboard.wasCanceled)
			{
				closeKeyboard();
			}
		}
	}

	public void closeKeyboard()
	{
		if (mKeyboard != null)
		{
			mKeyboard.active = false;
			mKeyboard = null;
		}
	}

	public void createKeyboard()
	{
		mKeyboard = TouchScreenKeyboard.Open(string.Empty, TouchScreenKeyboardType.Default, false, false);
	}

	private void OnDestroy()
	{
		closeKeyboard();
		FyberWrapper.adsInProgress -= AdsInProgressEventHandler;
	}

	public void showMainMenu()
	{
		hideAllPanelMenu();
		SetActiveIndictorExp(true);
		panelMainMenu.gameObject.SetActive(true);
		ActivityIndicator.activEnabled = false;
	}

	public void showWindowPVP()
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("showWindowPVP");
		}
		if (ReplacePerelivController.instance != null)
		{
			ReplacePerelivController.instance.pvpMapsScreenDelay = true;
		}
		settings.updateOfflineMode(false);
		ShowWindowPVP_AfterConnect();
		if (!PhotonNetwork.connected)
		{
			controllerConnectPhoton.thisScript.tekConnect = controllerConnectPhoton.connectTo.customGame;
			controllerConnectPhoton.thisScript._initializeWorldwide();
		}
	}

	public void ShowWindowPVP_AfterConnect()
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("ShowWindowPVP_AfterConnect");
		}
		clickOnButton = false;
		hideAllPanelMenu();
		controllerConnectPhoton.thisScript.tekConnect = controllerConnectPhoton.connectTo.customGame;
		controllerConnectPhoton.thisScript._initializeWorldwide();
		controllerConnectPhoton.thisScript.chooseNameLevel = string.Empty;
		controllerConnectPhoton.thisScript.chooseTypeGame = TypeGame.deathmath;
		SetActiveIndictorExp(true);
		if (!settings.isNewPvpPanelEnabled)
		{
		}
		X3AndBonusInMenu.instance.CanShowMessage();
	}

	public void ShowWindowRoomsPVP()
	{
		BCWDebug.Log("ShowWindowRoomsPVP");
		hideAllPanelMenu();
		if (controllerConnectPhoton.thisScript != null)
		{
			controllerConnectPhoton.thisScript.filterForSearchRoom = string.Empty;
			controllerConnectPhoton.thisScript.chooseTypeGame = TypeGame.deathmath;
		}
		SetActiveIndictorExp(true);
	}

	public void ShowWindowRoomsSandbox()
	{
		BCWDebug.Log("ShowWindowRoomsSandbox");
		hideAllPanelMenu();
		if (controllerConnectPhoton.thisScript != null)
		{
			controllerConnectPhoton.thisScript.chooseNameLevel = string.Empty;
			controllerConnectPhoton.thisScript.filterForSearchRoom = string.Empty;
			controllerConnectPhoton.thisScript.chooseTypeGame = TypeGame.normal;
		}
		SetActiveIndictorExp(true);
	}

	public void showCreateRoomPVP()
	{
		Save.SaveString(settings.keyEnterPassword, string.Empty);
		hideAllPanelMenu();
		controllerConnectPhoton.thisScript.chooseNameLevel = ManagerPVPLevels.instance.GetLevelInfo(0).nameScene;
		SetActiveIndictorExp(true);
	}

	public void showCreateRoomSandbox()
	{
		Save.SaveString(settings.keyEnterPassword, string.Empty);
		hideAllPanelMenu();
		SetActiveIndictorExp(true);
	}

	public void ShowProfile()
	{
		if (_showHeroRoom == null)
		{
			_showHeroRoom = StartCoroutine(showHeroRoom());
		}
	}

	public IEnumerator showHeroRoom()
	{
		if (panelHeroRoom == null)
		{
			LoadingPanel.instance.ShowLoadingPanel(true);
			ResourceRequest panel = Resources.LoadAsync("Prefabs/Lobby/PanelProffile");
			yield return panel;
			GameObject tmp = Object.Instantiate(panel.asset as GameObject);
			panelHeroRoom = tmp.GetComponent<UIPanel>();
			tmp.transform.parent = parentLobbyUI.transform;
			tmp.transform.localScale = Vector3.one;
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		hideAllPanelMenu();
		panelHeroRoom.gameObject.SetActive(true);
		ProfileCounters.instance.UpdateAllCounters();
		ProfilePanelUI.instance.UpdateProfileUI();
		panelHeroRoom.alpha = 1f;
		_showHeroRoom = null;
	}

	public void SetActiveIndictorExp(bool val)
	{
		if (indicatorExp != null)
		{
			indicatorExp.SetActive(val);
		}
	}

	private void OnApplicationPause(bool pauseStatus)
	{
		if (Load.LoadBool(settings.KeyActionAfterTutor) && pauseStatus)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary.Add("Action after tutor", "Get out");
			FlurryWrapper.LogEvent(FlurryWrapper.EV_TUTORIAL_CLOSE, dictionary);
			Save.SaveBool(settings.KeyActionAfterTutor, false);
		}
	}

	public void ShowSettings()
	{
		if (_showSettings == null)
		{
			_showSettings = StartCoroutine(showSettings());
		}
	}

	public IEnumerator showSettings()
	{
		if (Load.LoadBool(settings.KeyActionAfterTutor))
		{
			FlurryWrapper.LogEvent(parameters: new Dictionary<string, string> { { "Action after tutor", "Settings" } }, eventName: FlurryWrapper.EV_TUTORIAL_CLOSE);
			Save.SaveBool(settings.KeyActionAfterTutor, false);
		}
		if (panelSettings == null)
		{
			LoadingPanel.instance.ShowLoadingPanel(true);
			ResourceRequest settings2 = Resources.LoadAsync("Prefabs/Lobby/PanelSettings");
			yield return settings2;
			GameObject tmp = Object.Instantiate(settings2.asset as GameObject);
			panelSettings = tmp.GetComponent<UIPanel>();
			panelSettings.transform.parent = parentLobbyUI.transform;
			panelSettings.transform.localScale = Vector3.one;
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		hideAllPanelMenu();
		panelSettings.gameObject.SetActive(true);
		if (X3AndBonusInMenu.instance != null)
		{
			X3AndBonusInMenu.instance.CanShowMessage();
		}
		_showSettings = null;
	}

	public void ShowControllSettings()
	{
		if (_showControllSettings == null)
		{
			_showControllSettings = StartCoroutine(showControlsSettings());
		}
	}

	public IEnumerator showControlsSettings()
	{
		if (panelControlsSettings == null)
		{
			LoadingPanel.instance.ShowLoadingPanel(true);
			ResourceRequest controlSettings = Resources.LoadAsync("Prefabs/Lobby/PanelSettingsControl");
			yield return controlSettings;
			GameObject tmp = Object.Instantiate(controlSettings.asset as GameObject);
			panelControlsSettings = tmp.GetComponent<UIPanel>();
			panelControlsSettings.transform.parent = parentLobbyUI.transform;
			panelControlsSettings.transform.localScale = Vector3.one;
			LoadingPanel.instance.ShowLoadingPanel(false);
		}
		hideAllPanelMenu();
		panelControlsSettings.gameObject.SetActive(true);
		_showControllSettings = null;
	}

	public void showEnterPassword()
	{
	}

	public void ShowChooseCustomMode()
	{
		hideAllPanelMenu();
		SetActiveIndictorExp(true);
	}

	public void hideAllPanelMenu()
	{
		if (panelSettings != null)
		{
			panelSettings.gameObject.SetActive(false);
		}
		if (panelHeroRoom != null)
		{
			panelHeroRoom.gameObject.SetActive(false);
		}
		if (panelMainMenu != null)
		{
			panelMainMenu.gameObject.SetActive(false);
		}
		if (panelControlsSettings != null)
		{
			panelControlsSettings.gameObject.SetActive(false);
		}
		SetActiveIndictorExp(false);
	}

	public void startRandomGame()
	{
		showIndicator();
		settings.updateOfflineMode(false);
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty);
	}

	public void startRandomGame_PVP()
	{
		showIndicator();
		settings.updateOfflineMode(false);
		controllerConnectPhoton.thisScript.connectRandomRoom(null, TypeGame.deathmath);
	}

	public void startRandomGame_All()
	{
		showIndicator();
		settings.updateOfflineMode(false);
		int num = Random.Range(0, 100);
		if ((float)num < 100f / (float)ManagerPVPLevels.instance.levels.Count)
		{
			controllerConnectPhoton.thisScript.connectRandomRoom("Level1");
			controllerConnectPhoton.thisScript.chooseNameLevel = "Level1";
		}
		else
		{
			controllerConnectPhoton.thisScript.ConnectRandomRoom_PVP();
		}
	}

	public void StartRandomSandbox()
	{
		showIndicator();
		settings.updateOfflineMode(false);
		controllerConnectPhoton.thisScript.connectRandomRoom("Level1");
	}

	public void saveProfile()
	{
		PanelProffileName.instance.SaveNameProfile();
	}

	public void showIndicator(bool showDisconnect = false)
	{
		blockPanel.gameObject.SetActive(true);
	}

	public void hideIndicator()
	{
		if (blockPanel != null)
		{
			blockPanel.gameObject.SetActive(false);
		}
	}

	public void showMessage(string msg)
	{
		lbMessage.text = msg;
		Invoke("hideMessage", 3f);
		panelMessage.gameObject.SetActive(true);
	}

	public void hideMessage()
	{
		panelMessage.gameObject.SetActive(false);
	}

	public void playOfflineMode()
	{
		PhotonNetwork.Disconnect();
		settings.updateOfflineMode(true);
		settings.keySelectScene = "Level1";
		PlayerPrefs.Save();
		StartCoroutine(LoadBigWorld());
	}

	public void LoadAllOffline(TypeGame type)
	{
		PhotonNetwork.Disconnect();
		controllerLoadingGame.typeGame = type;
		settings.updateOfflineMode(true);
		settings.keySelectScene = ManagerPVPLevels.instance.GetRandomScene(type);
		PlayerPrefs.Save();
		StartCoroutine(LoadBigWorld());
	}

	private IEnumerator LoadBigWorld()
	{
		yield return SceneManager.LoadSceneAsync("LoadingGame");
	}

	public void CarWithAutoForwardOn()
	{
	}

	public void CarWithAutoForwardOff()
	{
	}

	public void CloseInfoAfterTutorial()
	{
		panelInfoAfterTutorial.SetActive(false);
		Save.SaveBool(settings.keyInfoAfterTutorial, false);
	}

	public void ShowInfoAfterTutorial()
	{
		if (Load.LoadBool(settings.keyInfoAfterTutorial))
		{
			panelInfoAfterTutorial.SetActive(true);
		}
	}

	public void BackToGameChose()
	{
		settings.playSoundButton();
		if (controllerConnectPhoton.thisScript.chooseTypeGame == TypeGame.deathmath)
		{
			ShowWindowRoomsPVP();
		}
		else
		{
			ShowWindowRoomsSandbox();
		}
	}
}
